package com.wyvoid.forum.web;

import com.wyvoid.forum.Constant;
import com.wyvoid.forum.domain.*;
import com.wyvoid.forum.service.IUserGroupService;
import com.wyvoid.forum.service.IUserService;
import com.wyvoid.forum.util.ForumUtil;
import net.sourceforge.stripes.action.ActionBeanContext;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * ActionBeanContext subclass for the mysite application that manages where values
 * like the logged in user are stored.
 * User: cjp
 * Date: 2006-7-19
 * Time: 22:05:04
 */
public class MyActionBeanContext extends ActionBeanContext {
    public MyActionBeanContext() {
    }


    public MyActionBeanContext(HttpServletRequest request, HttpServletResponse response) {
        this.setRequest(request);
        this.setResponse(response);
    }

    /**
     * Gets the currently logged in user, or null if no-one is logged in.
     *
     * @return User
     */


    public SessionUser getUser() {
        return (SessionUser) getRequest().getSession().getAttribute(Constant.SESSION_USER_KEY);
    }

    public long getUserId() {
        SessionUser sessionUser = getUser();
        if (sessionUser != null) {
            return sessionUser.getId();
        } else {
            return 0;
        }
    }

    /**
     * Sets the currently logged in user.
     *
     * @param currentUser currentUser
     */
    public void setUser(SessionUser currentUser) {
        getRequest().getSession().setAttribute(Constant.SESSION_USER_KEY, currentUser);
    }


    /**
     * Logs the user out by invalidating the session.
     */
    public void logout() {
        getRequest().getSession().removeAttribute(Constant.SESSION_USER_KEY);
        getRequest().getSession().invalidate();
    }

    public void setRequestAttribute(String name, Object obj) {
        getRequest().setAttribute(name, obj);
    }

    public Object getSessionAttribute(String name) {
        return getRequest().getSession().getAttribute(name);
    }

    public boolean isUserLoggedIn() {
        return getUser() != null;
    }

    public boolean isAdminLoggerIn() {
        return getAdmin() != null;
    }

    public SessionAdmin getAdmin() {
        return (SessionAdmin) getRequest().getSession().getAttribute(Constant.SESSION_ADMIN_KEY);
    }

    public void setAdmin(SessionAdmin sessionAdmin) {
        getRequest().getSession().setAttribute(Constant.SESSION_ADMIN_KEY, sessionAdmin);
    }

    public boolean canAccess(String authorityStr) {
        if (StringUtils.isBlank(authorityStr)) {
            return true;
        }
        String[] authrities = StringUtils.split(authorityStr, ",");

        IUserService userService = (IUserService) ForumUtil.context.getBean("userService");
        IUserGroupService userGroupService = (IUserGroupService) ForumUtil.context.getBean("userGroupService");

        SessionUser sessionUser = getUser();
        String curUserAuth = "";
        if (sessionUser != null) {
            User user = userService.getUser(sessionUser.getId());
            long groupId = user.getGroupId();
            UserGroup userGroup = userGroupService.getUserGroup(groupId);
            if (userGroup != null) {
                curUserAuth += userGroup.getPermissions();
            }

            long adminGroupId = user.getAdminId();
            AdminGroup adminGroup = userGroupService.getAdminGroup(adminGroupId);
            if (adminGroup != null) {
                curUserAuth += adminGroup.getPermissions();
            }
        } else {
            User user = userService.getUser("guest");
            long groupId = user.getGroupId();
            UserGroup userGroup = userGroupService.getUserGroup(groupId);
            if (userGroup != null) {
                curUserAuth += userGroup.getPermissions();
            }
        }


        String[] auths = StringUtils.split(curUserAuth, "|");

        return CollectionUtils.containsAny(Arrays.asList(auths), Arrays.asList(authrities));
    }

    public boolean isMaster() {
        if (!isUserLoggedIn()) {
            return false;
        }
        SessionUser sessionUser = getUser();
        IUserService userService = (IUserService) ForumUtil.context.getBean("userService");
        IUserGroupService userGroupService = (IUserGroupService) ForumUtil.context.getBean("userGroupService");
        User user = userService.getUser(sessionUser.getId());
        long adminGroupId=user.getAdminId();
        if(adminGroupId>0){
            return true;
        }
        long groupId = user.getGroupId();

        UserGroup userGroup = userGroupService.getUserGroup(groupId);
        return userGroup!=null&&userGroup.getAdminGroupId()>0;
    }
}
